﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace EWDT_CDS_Hub.App_Code
{
    public class Student
    {
        public String Student_id { get; set; }
        public String Student_name { get; set; }
        public String Student_email { get; set; }
        public String Care_group_id { get; set; }

        public static List<Student> GetStudent()
        {
            List<Student> results = new List<Student>();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();

                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM Student";
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    Student s = new Student();
                    s.Student_id = (String)dr["student_id"];
                    s.Student_name = (String)dr["student_name"];
                    s.Student_email = (String)dr["student_email"];
                    s.Care_group_id = (String)dr["care_group_id"];

                    results.Add(s);
                }
            }
            catch (SqlException e)
            {
            }
            finally
            {
                conn.Close();
            }

            return results;
        }

        public static int InsertStudent(Student s)
        {
            int rowsInserted = 0;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();

                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "INSERT INTO Student (student_id, student_name, student_email, care_group_id) VALUES (@id, @name, @email, @cgid)";
                comm.Parameters.AddWithValue("@id", s.Student_id);
                comm.Parameters.AddWithValue("@cgid", s.Care_group_id);
                comm.Parameters.AddWithValue("@name", s.Student_name);
                comm.Parameters.AddWithValue("@email", s.Student_email);
                rowsInserted = comm.ExecuteNonQuery();

            }
            catch (SqlException ex)
            {

            }
            finally
            {
                conn.Close();
            }

            return rowsInserted;
        }

        public static int UpdateStudent(Student s)
        {
            int rowsUpdated = 0;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "UPDATE Student SET student_name = @name, student_email = @email, care_group_id = @cgid WHERE student_id = @id";
                comm.Parameters.AddWithValue("@id", s.Student_id);
                comm.Parameters.AddWithValue("@name", s.Student_name);
                comm.Parameters.AddWithValue("@email", s.Student_email);
                comm.Parameters.AddWithValue("@cgid", s.Care_group_id);
                rowsUpdated = comm.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return rowsUpdated;
        }

        public static Student RetrieveStudent(String id)
        {
            Student s = null;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM Student WHERE student_id = @id";
                comm.Parameters.AddWithValue("@id", id);
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    s = new Student();
                    s.Student_id = (String)dr["student_id"];
                    s.Student_name = (String)dr["student_id"];
                    s.Student_email = (String)dr["student_email"];
                    s.Care_group_id = (String)dr["care_group_id"];
                }

                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return s;
        }

        public static Diploma GetStudentDiploma(String id)
        {
            Diploma d = null;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM Diploma WHERE diploma_id = " +
                                   "SELECT diploma_id FROM CareGroup WHERE care_group_id = " +
                                   "SELECT care_group_id FROM Student WHERE student_id = @id";
                comm.Parameters.AddWithValue("@id", id);
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    d = new Diploma();
                    d.Diploma_id = (String)dr["diploma_id"];
                    d.Diploma_name = (String)dr["diploma_name"];
                    d.School_id = (String)dr["school_id"];
                }

                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return d;
        }
    }
}